<?php

namespace app\admin\controller\report;

use app\common\controller\Backend;
use app\admin\model\ReportCategory;
use think\Db;
use Exception;

/**
 * 报表管理
 *
 * @icon fa fa-user
 */
class Report extends Backend
{

    protected $searchFields = 'id,name';

    /**
     * @var \app\admin\model\Report
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Report');
    }
	
    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
				->with('category')
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
			
            $result = array("total" => $list->total(), "rows" => $list->items());
			
            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
		if (false === $this->request->isPost()) {
			$options = [''=>'--请选择--'];
			$options = array_merge($options, ReportCategory::where('status', 1)->column('id,name'));
			$this->view->assign('reportTypeList', build_select('row[category_id]', $options, [], ['class' => 'form-control selectpicker', 'data-rule'=>'required']));
            return $this->view->fetch();
        }
		
		$this->token();
        $params = $this->request->post('row/a');
        if (empty($params)) {
            $this->error(__('Parameter %s can not be empty', ''));
        }

        $result = false;
        Db::startTrans();
        try {
            $result = $this->model->allowField(true)->save($params);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result === false) {
            $this->error(__('No rows were inserted'));
        }
        $this->success('添加成功', 'index');
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        if ($this->request->isPost()) {
            $this->token();
        }
        $row = $this->model->get($ids);
        $this->modelValidate = false;
        if (!$row) {
            $this->error(__('No Results were found'));
        }
		$options = [''=>'--请选择--'];
		$options = array_merge($options, ReportCategory::where('status', 1)->column('id,name'));
		$this->view->assign('reportTypeList', build_select('row[category_id]', $options, $row['category_id'], ['class' => 'form-control selectpicker', 'data-rule'=>'required']));
        return parent::edit($ids);
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        if (!$ids) {
            $this->error(__('Parameter %s can not be empty', 'ids'));
        }
        $count = $this->model->where('id', 'in', $ids)->delete();
		if ($count) {
			$this->success();
		}

        $this->error(__('No rows were deleted'));
    }
}
